//
//  AppDelegate.swift
//  WeiBoStudy
//
//  Created by luoerliu on 16/5/9.
//  Copyright © 2016年 刘文平. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//     创建UIWindow
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        let tabBarVC = luoerTabbarController()
        tabBarVC.view.backgroundColor = UIColor.whiteColor()
//        设置根控制器
//        window?.rootViewController = tabBarVC
//        let welcomeVC = luoerWelcomeViewController()
        window?.rootViewController = defaultViewController()
//        设置导航栏的皮肤
        let Bar = UINavigationBar.appearance()
        Bar.tintColor = UIColor.orangeColor()
//        设置为主窗口并且显示
        window?.makeKeyAndVisible()
        return true
    }
//    显示默认控制器的方法
    private func defaultViewController()->UIViewController
    {
//        如果用户没有登录
        if !luoerUserAccountViewModel.sharedinstance.isUserLogin
        {
//            如果用户没有登录则返回访客视图
            return luoerTabbarController()
        }
//        判断是否是新版本
        if isNewVersion()
        {
//            如果是新版本就返回新特性界面
            return luoerNewFeatureViewController()
        }
        else
        {
//            如果不是新版本就返回欢迎界面
             return luoerWelcomeViewController()
        }
    }
//    自定义一个方法让别人来切换根控制器
    func switchRootViewController(controller:UIViewController)
    {
//         将别人传入的控制器作为根控制器
        window?.rootViewController = controller
    }
//   判断当前应用是否是新版本
    private func isNewVersion()->Bool
    {
//        获取当前版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleInfoDictionaryVersion"] as! String
//        获取上一个版本号
        let sanboxVersionKey = "sanboxVersionKey"
        let sanboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sanboxVersionKey)
//        把两个版本号进行比较
        let isNewVersion = currentVersion != sanboxVersion
//        如果是新版本，就把当前的版本号保存到沙盒中
        if isNewVersion
        {
             NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sanboxVersionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        return isNewVersion
    }
    func applicationWillResignActive(application: UIApplication) {

    }

    func applicationDidEnterBackground(application: UIApplication) {
      
    }

    func applicationWillEnterForeground(application: UIApplication) {

    }

    func applicationDidBecomeActive(application: UIApplication) {
     
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

